# /******************************************************************
# *
# * Copyright 2017 Vprime All Rights Reserved.
# *
# *
# *
# * Licensed under the Apache License, Version 2.0 (the "License");
# * you may not use this file except in compliance with the License.
# * You may obtain a copy of the License at
# *
# *      http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# *
# *****************************************************************
# \author    Rami Alshafi <ralshafi@vprime.com>
# */

Import('env')
import os
import os.path
import platform as p
target_os = env.get('TARGET_OS')
target_arch = env.get('TARGET_ARCH')
samples_env = env.Clone()
src_dir = env.get('SRC_DIR')
build_dir = env.get('BUILD_DIR')

######################################################################
# Build flags
######################################################################
samples_env.PrependUnique(CPPPATH=[
    '#/resource/include/',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/stack/include/experimental',
    '#/resource/csdk/security/include',
    '#/resource/csdk/connectivity/api',
    '#/resource/oc_logger/include',
    '#/resource/csdk/logger/include/experimental'
])
cpp_defines = ['__WITH_DTLS__', 'TB_LOG']
libraries = ['octbstack', 'oc']
# identify which hardware the samples will be running on.
platform_info = p.platform()
joule = 'Linux' in platform_info and 'joule' in platform_info and \
'Ubuntu' in platform_info
raspberry_pi = 'Linux' in platform_info and \
('armv7l' in platform_info or 'armv6l' in platform_info) and \
'debian' in platform_info
if joule or raspberry_pi:
    conf = Configure(env)
    if not conf.CheckCXXHeader('mraa.hpp'):
        print("required library mraa not installed! ")
        if joule and not raspberry_pi:
            print(" To install mraa\n\
                $sudo add-apt-repository ppa:mraa/mraa\n\
                $sudo apt-get update\n\
                $sudo apt-get install libmraa1 libmraa-dev\
                 mraa-tools python-mraa python3-mraa\n")
        elif not joule and raspberry_pi:
            print("to install mraa\n\
                $git clone https://github.com/intel-iot-devkit/\
                mraa.git ../mraa\n\
                $mkdir ../mraa/build && cd ../mraa/build && cmake .. && make\n\
                $sudo make install")
        else:
            print("please install mraa")
    else:
        cpp_defines.append('WITH_MRAA')
        libraries.append('mraa')
        if joule and not raspberry_pi:
            cpp_defines.append('JOULE')
        elif not joule and raspberry_pi:
            cpp_defines.append('RASPBERRY')
        else:
            print("ERROR: unknown board")
    conf.Finish()
samples_env.AppendUnique(RPATH=[build_dir])
samples_env.AppendUnique(CPPDEFINES=cpp_defines)
samples_env.AppendUnique(CFLAGS=['-std=c99'])
samples_env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall', '-Wextra', '-Werror',
                                   '-pthread', '-fpermissive'])
samples_env.PrependUnique(LIBS=libraries)
if target_arch in ['x86_64', 'arm64']:
    samples_env.AppendUnique(CPPFLAGS=['-Llib64'])
else:
    samples_env.AppendUnique(CPPFLAGS=['-Llib'])
if env.get('SECURED') == '1':
    samples_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])
if target_os in ['linux']:
    samples_env.ParseConfig('pkg-config --cflags --libs sqlite3')

######################################################################
# Source files and Targets
######################################################################
client = samples_env.Program('client', ['client.c'])
server = samples_env.Program('server', ['server.cpp'])
examples_dir = '/examples/OCFSecure/'
client_dat = samples_env.Install(
    build_dir + examples_dir, src_dir + examples_dir + 'ocf_svr_db_client.dat')
client_dev = samples_env.Install(
    build_dir + examples_dir, src_dir + examples_dir + 'device_properties.dat')
server_dat = samples_env.Install(
    build_dir + examples_dir, src_dir + examples_dir + 'ocf_svr_db_server.dat')
introspection_dat = samples_env.Install(
    build_dir + examples_dir, src_dir + examples_dir + 'switch_introspection.dat')

list_of_samples = [client, client_dat, client_dev, server, server_dat]

Alias("secureExamples", list_of_samples)

env.AppendTarget('secureExamples')
